草庐IT

mongodb - 社交游戏 map 图 block 存储

全部标签

ruby - 用 RSpec 包裹在 block 中的测试方法

在我实际操作的简化示例中,假设我有2次对数据库的调用:Repo.add(something_stringy)Repo.remove(something_floaty)我想对数据库调用使用mock,因为真正的调用将在别处进行测试:let(:repo){repo=double("Repo")repo.should_receive(:add).with(instance_of(String))repo.should_receive(:remove).with(instance_of(Float))repo}before{FakeKlass.const_set:Repo,repo}这一切都很好

ruby - 是什么导致我的 Ruby `trap` block 出现这种死锁?

我正在通读JesseStorimer的优秀著作,WorkingwithUnixProcesses.在有关从已退出的子进程捕获信号的部分中,他提供了一个代码示例。我稍微修改了该代码(见下文)以更清楚地了解正在发生的事情:父级在信号之间恢复自己的执行(我可以通过它的puts看到),wait在一个trap语句中为多个child执行(有时我得到“收到CHLD信号”,然后是多个“childpid退出”)。预期输出通常下面代码的输出类似于:parentisworkinghardReceivedaCHLDsignalchildpid73408exitedparentisworkinghardpare

ruby - 为什么我们在 Rails 记录器中使用 block 而不是字符串?

在Rails中,当我们使用Logger类时,我们总是在block中定义而不是String-Rails.logger.error{error.message}不是按照下面的方式-Rails.logger.error"error.message"背后的原因是什么? 最佳答案 查看此处的文档:ImpactofLogsonPerformanceAnotherpotentialpitfallisthatifyouhavemanycallstoLoggerlikethisinyourcode:logger.debug"Personattribu

ruby - 从 Mechanize 中删除以前存储的 cookie

有没有一种方法或任何其他方式可以用来删除所有以前存储在Mechanize中的cookie?它记得我已经登录,这对我来说不是一件好事。每次连接到该页面时,我都需要登录。 最佳答案 在类似的东西之后agent=Mechanize.newagent.getSOMEURL您可以使用agent.cookie_jar.clear!删除所有cookie,这样代理的下一个请求将不会发送任何cookie。 关于ruby-从Mechanize中删除以前存储的cookie,我们在StackOverflow上找

ruby - RSpec 模拟 :each block

我想使用RSpec模拟来为block提供固定输入。ruby:classParserattr_accessor:extracteddefparse(fname)File.open(fname).eachdo|line|extracted=lineifline=~/^RCSfile:(.*),v$/endendendR规范:describeParserbeforedo@parser=Parser.new@lines=mock("lines")@lines.stub!(:each)File.stub!(:open).and_return(@lines)endit"shouldextracta

ruby - 如何测试一个 block 是否为空?

我有一段代码,我想在不运行代码块内部的情况下测试正文是否为空。这可能吗? 最佳答案 sourcifygem添加了一个Proc#to_source方法:>>require'sourcify'=>true>>p=Proc.new{}=>#>>p.to_source=>"proc{}"一旦将block作为字符串,就很容易看出花括号之间是否有注释(或只有空格)。 关于ruby-如何测试一个block是否为空?,我们在StackOverflow上找到一个类似的问题: h

ruby - ruby 中没有参数的 DSL block

我正在用ruby​​编写一个简单的dsl。几周前,我偶然发现了一些博客文章,其中展示了如何转换代码,例如:some_methodargumentdo|book|book.some_method_on_bookbook.some_other_method_on_book:with=>argumentend更简洁的代码:some_methodargumentdosome_method_on_booksome_other_method_on_book:with=>argumentend我不记得如何做到这一点,我也不确定缺点,但更简洁的语法很诱人。有人知道这种转变吗?

ruby-on-rails - 如何使用 block 创建助手?

我想做一个像下面这样的助手。defmy_divsome_options,&block#HowdoIprinttheresultoftheblock?end 最佳答案 你应该使用CaptureHelper.defmy_div(some_options,&block)#capturethevalueoftheblockastringcontent=capture(&block)#concatthevaluetotheoutputconcat(content)endThecontentdefmy_div(some_options,&blo

ruby - 在 block /lambda 中产生问题

我有以下Ruby代码:#func1generatesasequenceofitemsderivedfromx#func2doessomethingwiththeitemsgeneratedbyfunc1deftest(x,func1,func2)func1.call(x)do|y|func2.call(y)endendfunc1=lambdado|x|foriin1..5yieldx*iendendfunc2=lambdado|y|putsyendtest(2,func1,func2)#Shouldprint'2','4','6','8',and'10'这当然行不通。test.rb:1

ruby - 为什么只有有限数量的正则表达式捕获存储在 `global_variables` 中?

如果我用包含十个捕获的正则表达式进行匹配:/(o)(t)(th)(f)(fi)(s)(se)(e)(n)(t)/.match("otthffisseent")然后,对于$10,我得到:$10#=>"t"但global_variables中缺少它。我得到(在irbsession中):[:$;,:$-F,:$@,:$!,:$SAFE,:$~,:$&,:$`,:$',:$+,:$=,:$KCODE,:$-K,:$,,:$/,:$-0,:$\,:$_,:$stdin,:$stdout,:$stderr,:$>,:$这里只列出前九个:$1,:$2,:$3,:$4,:$5,:$6,:$7,:$8,